import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 				= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms : [
			{ label: '物流人员',			name: 'empl_id', type: 'select', loading:true },
			{ label: '派送状态',			name: 'outsourcing_status', type: 'select', loading:true },
			{ label: '外送时间',			names: ['start_date','end_date'], type:'date-range' },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'time-null', type:'date-range' },
			{ label: '条码号',			name: 'spec_code' },
			{ label: '科室 ',			name: 'project_parent_id', 		type:'select', idStr:'uuid' },
			{ label: '岗位',				name: 'project_id', 			type:'select' }
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.getSelect()
		
		if(window.isInit){
			this.fetch()
		}
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 物流人员
			$fn.setRow(forms, 'empl_id', await f.getCache({ api:'employee' } ) )
			// 派送状态
			const outsourceData = $fn.setRow(forms, 'outsourcing_status', await f.outsourcing_state )
			// 科室
			$fn.setRow(forms, 'project_parent_id', await f.getCache({ api:'ke_shi' } ) )
			// 岗位
			$fn.setRow(forms, 'project_id', await f.getCache({ api:'gang_wei' } ) )
			
			this.setState({ forms, outsourceData})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill-st/outsourcingList', {isIndex:true})
	
	// 表单
	cols = [
		{ title: '外送单位',				dataIndex: 'outsourcing_company', 		width:150 },
		{ title: '外送时间',				dataIndex: 'outsourcing_created_at', 	...$fn.table.time },
		{ title: '派送状态',				dataIndex: 'outsourcing_status', 		width:100 , render: text=>{
			if($fn.hasArray(this.state.outsourceData)){
				const rows = this.state.outsourceData.filter(v=> +v.value === +text)
				return $fn.hasArray(rows) ? rows[0]['name'] : '--'
			}
		}},
		{ title: '送达时间',				dataIndex: 'outsourcing_updated_at', 	...$fn.table.time },
		{ title: '外送人员',				dataIndex: 'outsourcing_empl', 			...$fn.table.name },
		{ title: '岗位',					dataIndex: 'project_name', 				width:150 },
		{ title: '接收时间',				dataIndex: 'check_time', 				...$fn.table.time },
		{ title: '客户名称',				dataIndex: 'hosp_name', 				width:250 },
		{ title: '条码号',				dataIndex: 'old_spec_code', 			...$fn.table.spec_code },
		{ title: '自然项目',				dataIndex: 'parent_kind_name', 			width:250 },
		{ title: '项目名称',				dataIndex: 'kind_name', 				width:250 },
		{ title: '标准价格',				dataIndex: 'sprice', 					...$fn.table.num },
		{ title: '实际价格',				dataIndex: 'price', 					...$fn.table.num },
		// 新增初始结算价字段 20211018 耿琰
		{ title: '初始结算价',			dataIndex: 'initial_price', 			...$fn.table.num },
		{ title: '姓名',					dataIndex: 'patient_name', 				...$fn.table.name },
		{ title: '性别',					dataIndex: 'sex_name', 					...$fn.table.sex },
		{ title: '年龄',					dataIndex: 'age', 						...$fn.table.age },
		{ title: '送检科室',				dataIndex: 'department_name', 			width:150 },
		{ title: '医生',					dataIndex: 'doctor', 					...$fn.table.name },
		{ title: '备注',					dataIndex: 'remark', 					width:150 },
		{ title: '账单属性',				dataIndex: 'attribute_name', 			width:100 },
		{ title: '实验科室', 			dataIndex: 'project_parent_name', 		width:100 },
		{ title: '开票单位（NC）',		dataIndex: 'organization_name', 		width:150 },
		{ title: '开票单位编码（NC）',	dataIndex: 'organization_code', 		width:150 },
		{ title: '账单创建日期', 	dataIndex: 'bill_created_at', 		...$fn.table.time },
		{ title: '业务员',				dataIndex: 'salesman_user', 			...$fn.table.name },
		{ title: '业务员编码',			dataIndex: 'salesman_code', 			width:150 },
		{ title: '区域',					dataIndex: 'region_name', 				width:150 },
		{ title: '区域编码',				dataIndex: 'region_num', 				...$fn.table.num }
	]
	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this, 'bill-st/outsourcingList', { isSock:true })					// 导出
		]
	}
	render(){
		return (
			<>
				<Page title='外包清单管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{ isIndex:true, isInit: window.isInit }) }
				</Page>
			</>
		)
	}
}

export default Index